<template>
  <div class="answer" @scroll="loadMore">
    <div v-for="item in answer" :key="item.id">
      <div class="answer-content">
        <div class="answer-author">
          <img :src="`https://ss.lanqb.com/${item.profile.avatar}`" alt="" />
          <span>{{ item.profile.nickname }}</span>
        </div>
        <div class="answer-title">
          <div>问</div>
          <h3>{{ item.title }}</h3>
        </div>
        <div class="answer-title-content">
          <p>{{ item.content }}</p>
          <img :src="`https://ss.lanqb.com/${item.works[0].url}`" alt="" />
        </div>
        <div class="answer-bottom">
          <div class="answer-bottom-img">
            <img
              :src="`https://ss.lanqb.com/${item.adopt_answer.avatar}`"
              alt=""
            />
            <p>{{ item.adopt_answer.nickname }}</p>
            <span>已采纳</span>
          </div>
          <p class="answer-bottom_content">
            {{ item.adopt_answer.content[0].value }}
          </p>
        </div>
      </div>
    </div>
    <div v-show="isShow" style="margin-top: 20px; text-align: center">
      加载中...
    </div>
  </div>
</template>

<script>
export default {
  name: "CommunityAnswer",

  data() {
    return {
      answer: [],
      isShow: true,
      curPage: 1,
    };
  },

  methods: {
    // 触底加载
    loadMore(e) {
      console.log("111", e.target.scrollTop);
      if (e.target.scrollTop + e.target.clientHeight >= e.target.scrollHeight) {
        this.curPage++;
        this.isShow = true;
        fetch(`https://m.lanqb.com/api/works/qa-list?page=${this.curPage}`)
          .then((res) => res.json())
          .then((data) => {
            this.answer = [...this.answer, ...data.works];
            console.log(this.answer);
            this.isShow = false;
          });
      }
    },
  },
  async created() {
    const {
      data: { works },
    } = await this.$request("/lqb/api/works/qa-list", "GET", {
      params: {
        page: 1,
      },
    });
    this.answer = works;
    console.log(this.answer);
  },
};
</script>

<style lang="scss">
.answer {
  height: calc(100vh - 48px - 44px - 60px);
  overflow-y: scroll;
  // width: 90%;

  margin: 0 auto;
  .answer-content {
    padding: 20px;
    border-top: 3px solid #eee;
    .answer-author {
      margin-top: 10px;
      height: 35px;
      display: flex;
      align-items: center;
      > img {
        height: 35px;
        height: 35px;
        border-radius: 50%;
      }
      span {
        margin-left: 5px;
      }
    }
    .answer-title {
      display: flex;
      margin-top: 15px;
      > div {
        width: 24px;
        height: 24px;
        line-height: 24px;
        text-align: center;
        margin-right: 5px;
        border-radius: 10px;
        background: linear-gradient(324deg, #008bff, #006bfe);
      }
      h3 {
        color: #1a1b1e;
      }
    }
    .answer-title-content {
      height: 72px;
      margin-top: 15px;
      display: flex;
      justify-content: space-between;
      p {
        width: 75%;
        margin-top: 15px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical; /*设置对齐模式*/
        -webkit-line-clamp: 3; /*设置多行的行数，示例为2行*/
      }
      img {
        width: 71px;
        height: 71px;
        border-radius: 10px;
      }
    }
    .answer-bottom {
      height: 76px;
      padding: 20px;
      margin-top: 15px;
      background-color: #f7f7f7;
      border-radius: 10px;
      .answer-bottom-img {
        display: flex;
        align-items: center;
        img {
          width: 22px;
          height: 22px;
          border-radius: 50%;
        }
        p {
          font-size: 18px;
          margin-left: 10px;
        }
        span {
          width: 70px;
          display: block;

          background: linear-gradient(90deg, #ffed70, #ffd865);
          line-height: 22px;
          margin-left: 10px;
          border-radius: 10px;
          text-align: center;
        }
      }
    }
    .answer-bottom_content {
      margin-top: 20px;
      font-size: 18px;
      overflow: hidden;
      text-overflow: ellipsis;
      display: -webkit-box;
      -webkit-box-orient: vertical; /*设置对齐模式*/
      -webkit-line-clamp: 2; /*设置多行的行数，示例为2行*/
    }
  }
}
</style>
